﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace CommonManagement.EntityFrameworkCore
{
    /// <summary>
    /// 公共管理数据上下文模型创建扩展类
    /// </summary>
    public static class CommonManagementDbContextModelCreatingExtensions
    {
        public static void ConfigureCommonManagement(
            this ModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            modelBuilder.Entity<User>(b =>
            {
                b.ToTable("Users");

                b.ConfigureByConvention();

                b.Property(x => x.UserName).IsRequired();
                b.Property(x => x.Password).IsRequired();
                b.Property(x => x.Sex).IsRequired();
                b.Property(x => x.Phonenumber).IsRequired();
                b.Property(x => x.Balance).HasColumnType("decimal(18,2)");

                b.HasIndex(x => x.UserName).IsUnique(true);

                // 用户、余额变动记录一对多
                b.HasMany(f => f.Balances).WithOne(f => f.User);
                // 用户、充值记录一对多
                b.HasMany(f => f.Recharges).WithOne(f => f.User);
                // 用户、积分记录一对多
                b.HasMany(f => f.Scores).WithOne(f => f.User);
                // 用户、获取积分消息一对多
                b.HasMany(f => f.Notices).WithOne(f => f.User);
                // 用户、反馈一对多
                b.HasMany(f => f.Feedbacks).WithOne(f => f.User);
                // 用户、评论一对多
                b.HasMany(f => f.Comments).WithOne(f => f.User);
                // 用户、订单一对多
                b.HasMany(f => f.Orders).WithOne(f => f.User);
            });

            modelBuilder.Entity<Balance>(b =>
            {
                b.ToTable("Balances");

                b.ConfigureByConvention();

                b.Property(x => x.ChangeType).IsRequired();
                b.Property(x => x.Event).IsRequired();
                b.Property(x => x.ChangeAmount).HasColumnType("decimal(18,2)");
                b.Property(x => x.ChangeTime).HasDefaultValueSql("GETDATE()");

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Recharge>(b =>
            {
                b.ToTable("Recharges");

                b.ConfigureByConvention();

                b.Property(x => x.RechargeDate).HasDefaultValueSql("GETDATE()");

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Score>(b =>
            {
                b.ToTable("Scores");

                b.ConfigureByConvention();

                b.Property(x => x.ChangeDate).HasDefaultValueSql("GETDATE()");

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Level>(b =>
            {
                b.ToTable("Levels");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
            });

            modelBuilder.Entity<Notice>(b =>
            {
                b.ToTable("Notices");

                b.ConfigureByConvention();

                b.Property(x => x.ChangeDate).HasDefaultValueSql("GETDATE()");

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Feedback>(b =>
            {
                b.ToTable("Feedbacks");

                b.ConfigureByConvention();

                b.Property(x => x.Title).IsRequired();
                b.Property(x => x.Content).IsRequired();

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Category>(b =>
            {
                b.ToTable("Categories");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.Sort).HasDefaultValueSql("0");

                // 分类、新闻一对多
                b.HasMany(f => f.Presses).WithOne(f => f.Category);
            });


            modelBuilder.Entity<Comment>(b =>
            {
                b.ToTable("Comments");

                b.ConfigureByConvention();

                b.Property(x => x.Content).IsRequired();
                b.Property(x => x.LikeNum).HasDefaultValueSql("0");
                b.Property(x => x.CommentDate).HasDefaultValueSql("GETDATE()");
            });

            modelBuilder.Entity<Press>(b =>
            {
                b.ToTable("Presses");

                b.ConfigureByConvention();

                b.Property(x => x.Cover).IsRequired();
                b.Property(x => x.Title).IsRequired();
                b.Property(x => x.SubTitle).IsRequired();
                b.Property(x => x.Content).IsRequired();
                b.Property(x => x.LikeNum).HasDefaultValueSql("0");
                b.Property(x => x.ReadNum).HasDefaultValueSql("0");
                b.Property(x => x.PublishDate).HasDefaultValueSql("GETDATE()");

                // 新闻、评论一对多
                b.HasMany(f => f.Comments).WithOne(f => f.Press);
            });

            modelBuilder.Entity<Rotation>(b =>
            {
                b.ToTable("Rotations");

                b.ConfigureByConvention();

                b.Property(x => x.AdvTitle).IsRequired();
                b.Property(x => x.AdvImg).IsRequired();
                b.Property(x => x.ServModule).IsRequired();
                b.Property(x => x.Sort).HasDefaultValueSql("0");
            });

            modelBuilder.Entity<Province>(b =>
            {
                b.ToTable("Provinces");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();

                // 省份、城市一对多
                b.HasMany(f => f.Cities).WithOne(f => f.Province);
            });

            modelBuilder.Entity<City>(b =>
            {
                b.ToTable("Citys");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();

                b.HasIndex(x => x.ProvinceId).IsUnique(false);

                // 城市、区域一对多
                b.HasMany(f => f.Areas).WithOne(f => f.City);
            });

            modelBuilder.Entity<Area>(b =>
            {
                b.ToTable("Areas");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();

                b.HasIndex(x => x.CityId).IsUnique(false);

                // 区域、位置一对多
                b.HasMany(f => f.Locations).WithOne(f => f.Area);
            });

            modelBuilder.Entity<Location>(b =>
            {
                b.ToTable("Locations");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
            });

            modelBuilder.Entity<Service>(b =>
            {
                b.ToTable("Services");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.Desc).IsRequired();
                b.Property(x => x.ImgUrl).IsRequired();
                b.Property(x => x.Link).IsRequired();
                b.Property(x => x.Sort).HasDefaultValueSql("0");
            });

            modelBuilder.Entity<Product>(b =>
            {
                b.ToTable("Products");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.Cover).IsRequired();
                b.Property(x => x.Description).IsRequired();
            });

            modelBuilder.Entity<Order>(b =>
            {
                b.ToTable("Orders");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.No).IsRequired();
                b.Property(x => x.Type).IsRequired();
                b.Property(x => x.TypeName).IsRequired();

                b.HasIndex(x => x.UserId).IsUnique(false);
            });

            modelBuilder.Entity<Weather>(b =>
            {
                b.ToTable("Forecasts");

                b.ConfigureByConvention();

                b.Property(x => x.UpdateTime).IsRequired();

                // 天气、温湿度一对一
                b.HasOne(f => f.TempInfo).WithOne(f => f.Forecast);
                // 天气、风力一对一
                b.HasOne(f => f.Wind).WithOne(f => f.Forecast);
                // 天气、舒适度一对一
                b.HasOne(f => f.ComfortLevel).WithOne(f => f.Forecast);
                // 天气、空气质量一对一
                b.HasOne(f => f.AirQuantity).WithOne(f => f.Forecast);
                // 天气、分时一对多
                b.HasMany(f => f.Divisions).WithOne(f => f.Forecast);
            });

            modelBuilder.Entity<AirQuantity>(b =>
            {
                b.ToTable("AirQuantities");

                b.ConfigureByConvention();

                b.Property(x => x.Id).IsRequired().ValueGeneratedNever();
                b.Property(x => x.Air).IsRequired();
            });

            modelBuilder.Entity<ComfortLevel>(b =>
            {
                b.ToTable("ComfortLevels");

                b.ConfigureByConvention();

                b.Property(x => x.Id).IsRequired().ValueGeneratedNever();
                b.Property(x => x.DressingIndex).IsRequired();
                b.Property(x => x.ColdIndex).IsRequired();
                b.Property(x => x.WashIndex).IsRequired();
                b.Property(x => x.SportIndex).IsRequired();
                b.Property(x => x.Uv).HasDefaultValueSql("0");
            });

            modelBuilder.Entity<TempInfo>(b =>
            {
                b.ToTable("TempInfos");

                b.ConfigureByConvention();

                b.Property(x => x.Id).IsRequired().ValueGeneratedNever();
                b.Property(x => x.Weather).IsRequired();
            });

            modelBuilder.Entity<Wind>(b =>
            {
                b.ToTable("Winds");

                b.ConfigureByConvention();

                b.Property(x => x.Id).IsRequired().ValueGeneratedNever();
                b.Property(x => x.WindStrength).IsRequired();
                b.Property(x => x.WindDirection).IsRequired();
            });

            modelBuilder.Entity<Division>(b =>
            {
                b.ToTable("Divisations");

                b.ConfigureByConvention();

                b.Property(x => x.Hour).IsRequired();
                b.Property(x => x.Weather).IsRequired();

                b.HasIndex(x => x.ForecastId).IsUnique(false);
            });

        }
    }
}